﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace CrashSimul8or
{
    class ClickHandler
    {
        // Þetta eru bara temp breytur til að geyma allskonar dót, þegar það er ýtt á takka t.d. þá vil ég að
        // forritið geymi þær upplýsingar til að ég geti notað þær þegar það er sleppt takkanum. Þannig get ég
        // t.d. abuse-að það að það er hægt að halda inni takkanum og draga til að velja units.
        bool mousePressedLeft;
        bool mousePressedRight;
        bool mousePressedMiddle;
        // Og allskonar temp breytur sem geyma drag-dót fyrir mig.
        float dragTmpX;
        float dragTmpY;
        float dragTmpX2;
        float dragTmpY2;

        public ClickHandler()
        {

        }

        public void Update(List<Unit> units, List<int> selectedUnits, MouseState mouseState, int Width, int Height, Camera2d camera)
        {

            if (mouseState.RightButton == ButtonState.Pressed)
            {
                mousePressedRight = true;
            }

            // Smellt á sjálft mappið
            if (mouseState.RightButton == ButtonState.Released && mousePressedRight)
            {
                mousePressedRight = false;

                if (mouseState.X <= Width && mouseState.X >= 0 && mouseState.Y >= 0 && mouseState.Y <= Height - 128)
                {
                    foreach (int selectedUnit in selectedUnits)
                    {
                        if (GameVariables.soundEffects)
                        {
                            Random random = new Random();
                            switch (random.Next(0, 3))
                            {
                                case 0: ResourceManager.getSound("Yeah1").Play(); break;
                                case 1: ResourceManager.getSound("Yeah2").Play(); break;
                                case 2: ResourceManager.getSound("Yeah3").Play(); break;
                            }
                        }
                        units[selectedUnit].goTo(mouseState.X + (int)camera.Pos.X, mouseState.Y + (int)camera.Pos.Y);
                    }
                }

                // Smellt á minimap
                if (mouseState.X <= Width && mouseState.X >= Width - 180 && mouseState.Y >= Height - 128 && mouseState.Y <= Height)
                {
                    foreach (int selectedUnit in selectedUnits)
                    {
                        if (GameVariables.soundEffects)
                        {
                            Random random = new Random();
                            switch (random.Next(0, 3))
                            {
                                case 0: ResourceManager.getSound("Yeah1").Play(); break;
                                case 1: ResourceManager.getSound("Yeah2").Play(); break;
                                case 2: ResourceManager.getSound("Yeah3").Play(); break;
                            }
                        }

                        Vector2 newLoc = new Vector2(Convert.ToInt32(((mouseState.X - (Width - 180)) / 180.0) * 1600), Convert.ToInt32(((mouseState.Y - (Height - 128)) / 128.0) * 896));

                        units[selectedUnit].goTo(newLoc.X, newLoc.Y);

                        System.Console.WriteLine(mouseState.X);
                        System.Console.WriteLine(mouseState.Y);
                        System.Console.WriteLine(newLoc.X);
                        System.Console.WriteLine(newLoc.Y);
                    }
                }

            }

            if (mouseState.LeftButton == ButtonState.Pressed)
                mousePressedLeft = true;

            if (mouseState.LeftButton == ButtonState.Released && mousePressedLeft)
            {
                mousePressedLeft = false;

                if (mouseState.X <= Width && mouseState.X >= 0 && mouseState.Y >= 0 && mouseState.Y <= Height)
                {
                    selectedUnits.Clear();
                    // Höndlum klikk á units á mappi
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (mouseState.X + camera.Pos.X > units[i].X - units[i].Size / 2
                            && mouseState.X + camera.Pos.X < units[i].X + units[i].Size / 2
                            && mouseState.Y + camera.Pos.Y > units[i].Y - units[i].Size / 2
                            && mouseState.Y + camera.Pos.Y < units[i].Size / 2 + units[i].Y)
                        {
                            selectedUnits.Clear();
                            selectedUnits.Add(i);
                            if (GameVariables.soundEffects)
                            {
                                Random random = new Random();
                                switch (random.Next(0, 2))
                                {
                                    case 0: ResourceManager.getSound("Yo").Play(); break;
                                    case 1: ResourceManager.getSound("wimpyhello").Play(); break;
                                }
                            }
                        }
                    }
                    // Höndlum klikk á units á GUI
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (mouseState.X > i * 40
                            && mouseState.X < (i * 40) + 40
                            && mouseState.Y > Height - 32
                            && mouseState.Y < Height)
                        {
                            selectedUnits.Clear();
                            selectedUnits.Add(i);
                        }
                    }

                }
            }

            if (mouseState.MiddleButton == ButtonState.Pressed && !mousePressedMiddle)
            {
                mousePressedMiddle = true;
                dragTmpX = mouseState.X + camera.Pos.X;
                dragTmpY = mouseState.Y + camera.Pos.Y;
                dragTmpX2 = mouseState.X;
                dragTmpY2 = mouseState.Y;
            }

            if (mouseState.MiddleButton == ButtonState.Released && mousePressedMiddle)
            {
                mousePressedMiddle = false;
                float tmpX = mouseState.X + camera.Pos.X;
                float tmpY = mouseState.Y + camera.Pos.Y;

                selectedUnits.Clear();

                // Dregur niður og til hægri
                if (tmpX > dragTmpX && tmpY > dragTmpY)
                {
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (tmpX > units[i].X
                            && dragTmpX < units[i].X + units[i].Size
                            && tmpY > units[i].Y
                            && dragTmpY < units[i].Size + units[i].Y)
                            selectedUnits.Add(i);
                    }
                }
                // Dregur niður og til vinstri
                else if (tmpX < dragTmpX && tmpY > dragTmpY)
                {
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (tmpX < units[i].X
                            && dragTmpX > units[i].X + units[i].Size
                            && tmpY > units[i].Y
                            && dragTmpY < units[i].Size + units[i].Y)
                            selectedUnits.Add(i);
                    }
                }
                // Dregur upp og til hægri
                else if (tmpX > dragTmpX && tmpY < dragTmpY)
                {
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (tmpX > units[i].X
                            && dragTmpX < units[i].X + units[i].Size
                            && tmpY < units[i].Y
                            && dragTmpY > units[i].Size + units[i].Y)
                            selectedUnits.Add(i);
                    }
                }
                // Dregur upp og til vinstri
                else if (tmpX < dragTmpX && tmpY < dragTmpY)
                {
                    for (int i = 0; i != units.Count(); i++)
                    {
                        if (tmpX < units[i].X
                            && dragTmpX > units[i].X + units[i].Size
                            && tmpY < units[i].Y
                            && dragTmpY > units[i].Size + units[i].Y)
                            selectedUnits.Add(i);
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, MouseState mouseState, Camera2d camera)
        {
            if (mousePressedMiddle)
            {
                mouseState = Mouse.GetState();
                int tmpX = mouseState.X + (int)camera.Pos.X;
                int tmpY = mouseState.Y + (int)camera.Pos.Y;

                // Dregur niður og til hægri
                if (tmpX > dragTmpX && tmpY > dragTmpY)
                {
                    spriteBatch.Draw(ResourceManager.getTexture("Select"),
                    new Rectangle(
                        (int)dragTmpX,
                        (int)dragTmpY,
                        (int)mouseState.X - (int)dragTmpX2,
                        (int)mouseState.Y - (int)dragTmpY2),
                    Color.White);
                }
                // Dregur niður og til vinstri
                else if (tmpX < dragTmpX && tmpY > dragTmpY)
                {
                    spriteBatch.Draw(ResourceManager.getTexture("Select"),
                    new Rectangle(
                        (int)mouseState.X + (int)camera.Pos.X,
                        (int)dragTmpY,
                        (int)dragTmpX2 - (int)mouseState.X,
                        (int)mouseState.Y - (int)dragTmpY2),
                    Color.White);
                }
                // Dregur upp og til hægri
                else if (tmpX > dragTmpX && tmpY < dragTmpY)
                {
                    spriteBatch.Draw(ResourceManager.getTexture("Select"),
                    new Rectangle(
                        (int)dragTmpX,
                        (int)mouseState.Y + (int)camera.Pos.Y,
                        (int)mouseState.X - (int)dragTmpX2,
                        (int)dragTmpY2 - (int)mouseState.Y),
                    Color.White);
                }
                // Dregur upp og til vinstri
                else if (tmpX < dragTmpX && tmpY < dragTmpY)
                {
                    spriteBatch.Draw(ResourceManager.getTexture("Select"),
                    new Rectangle(
                        (int)mouseState.X + (int)camera.Pos.X,
                        (int)mouseState.Y + (int)camera.Pos.Y,
                        (int)dragTmpX2 - mouseState.X,
                        (int)dragTmpY2 - mouseState.Y),
                    Color.White);
                }
            }
        }
    }
}
